/*
 * Action.h
 *
 *	Author: stravant
 *	Description:
 *		
 */

#ifndef TASK_H_
#define TASK_H_

#include "../core/Time.h"
#include "../core/Tuple.h"

namespace tbt {

/*
 * class Action
 *  An action is used by an event loop to do work, they are owned BY THE CREATOR, not
 *  by the event loop to which they are posted, unless otherwise
 */
class Action {
public:
	Action();
	virtual ~Action();
	virtual void run() = 0;
};

class CancelableAction: public Action {
public:
	CancelableAction();
	virtual ~CancelableAction();
	virtual void cancel() = 0;
};

template <class T>
class DeleteAction: public CancelableAction {
public:
	DeleteAction(T* obj): mObject(obj) {}
	~DeleteAction() {}
	virtual void run() {
		delete mObject;
	}
	virtual void cancel() {
		mObject = 0;
	}
private:
	T* mObject;
};

template <class T, class Method, class Params>
class RunMethodAction: public CancelableAction {
public:
	RunMethodAction(T* obj, Method meth, const Params& params):
		mObject(obj), mMethod(meth), mParams(params) {}
	virtual void run() {
		if (mObject)
			DispatchToMethod(mObject, mMethod, mParams);
	}
	virtual void cancel() {
		mObject = 0;
	}
private:
	T* mObject;
	Method mMethod;
	Params mParams;
};

template <class T, class Method, class Params>
RunMethodAction<T,Method,Params>* NewRunMethodAction(T* obj, Method method, const Params& params) {
	return new RunMethodAction<T,Method,Params>(obj, method, params);
}

}

#endif /* TASK_H_ */




















